/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Yunzi
 */
@Entity
@Table(name = "imenu_menu")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "ImenuMenu.findAll", query = "SELECT i FROM ImenuMenu i"),
    @NamedQuery(name = "ImenuMenu.findById", query = "SELECT i FROM ImenuMenu i WHERE i.id = :id"),
    @NamedQuery(name = "ImenuMenu.findByUpdateTime", query = "SELECT i FROM ImenuMenu i WHERE i.updateTime = :updateTime"),
    @NamedQuery(name = "ImenuMenu.findByImageLandscape", query = "SELECT i FROM ImenuMenu i WHERE i.imageLandscape = :imageLandscape"),
    @NamedQuery(name = "ImenuMenu.findByImagePortrait", query = "SELECT i FROM ImenuMenu i WHERE i.imagePortrait = :imagePortrait"),
    @NamedQuery(name = "ImenuMenu.findByTitle", query = "SELECT i FROM ImenuMenu i WHERE i.title = :title"),
    @NamedQuery(name = "ImenuMenu.findByName", query = "SELECT i FROM ImenuMenu i WHERE i.name = :name")})
public class ImenuMenu implements Serializable {

    @Basic(optional = false)
    @NotNull
    @Column(name = "update_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updateTime;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", insertable = false)
    private Integer id;
    @Size(max = 100)
    @Column(name = "image_landscape")
    private String imageLandscape;
    @Size(max = 100)
    @Column(name = "image_portrait")
    private String imagePortrait;
    @Size(max = 40)
    @Column(name = "title")
    private String title;
    @Size(max = 40)
    @Column(name = "name")
    private String name;
    @Lob
    @Size(max = 65535)
    @Column(name = "description")
    private String description;
    
    @JoinColumn(name = "store_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private ImenuStore store;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "menu")
    private Collection<ImenuCourse> imenuCourseCollection;

    public ImenuMenu() {
    }

    public ImenuMenu(Integer id) {
        this.id = id;
    }

    public ImenuMenu(Integer id, Date updateTime) {
        this.id = id;
        this.updateTime = updateTime;
    }

    @XmlTransient
    public Collection<ImenuCourse> getImenuCourseCollection() {
        return imenuCourseCollection;
    }

    public void setImenuCourseCollection(Collection<ImenuCourse> imenuCourseCollection) {
        this.imenuCourseCollection = imenuCourseCollection;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public boolean isHasLandscapeImage() {
        return this.imageLandscape != null && this.imageLandscape.length() > 1;
    }

    public boolean isHasPortraitImage() {
        return this.imagePortrait != null && this.imagePortrait.length() > 1;
    }

    public String getSmallLandscape() {
        System.out.println("ImenuMenu.getSmallLandscape() this.imageLandscape = "+this.imageLandscape);
        if (this.isHasLandscapeImage()) {
            return "100x75_" + this.getImageLandscape();
        } else {
            return "../landscape.png";
        }
    }

    public String getSmallPortrait() {
        if (this.isHasPortraitImage()) {
            return "75x100_" + this.getImagePortrait();
        } else {
            return "../portrait.png";
        }
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getImageLandscape() {
        return imageLandscape;
    }

    public void setImageLandscape(String imageLandscape) {
        this.imageLandscape = imageLandscape;
    }

    public String getImagePortrait() {
        return imagePortrait;
    }

    public void setImagePortrait(String imagePortrait) {
        this.imagePortrait = imagePortrait;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public ImenuStore getStore() {
        return store;
    }

    public void setStore(ImenuStore store) {
        this.store = store;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ImenuMenu)) {
            return false;
        }
        ImenuMenu other = (ImenuMenu) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.imenu.entity.ImenuMenu[ id=" + id + " ]";
    }
}
